import scrapy

from ScrapyObject.spiders.utils.url_utils import *

''''
scrapy crawl ydaa -o ydaa.json
https://kcnn.cc/

a、b、c、d、e、f、g、h、i、j、k、l、m、n、o、p、q、r、s、t、u、v、w、x、y、z

scrapy genspider ydbd https://semao201.top/
'''


#     item['tags'] = tags
#     item['urls'] = url
#     item['names'] = name
#     item['picture_urls'] = pUrl
#     item['video_urls'] = vUrl
class YdaaSpider(scrapy.Spider):
    name = "ydaa"
    # 前缀
    prefix = 'https://'
    # 中缀
    website = 'kcnn'
    # 后缀
    suffix = '.cc/'
    allowed_domains = [website + '.cc']
    start_urls = [prefix + website + suffix]

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        video_url = get_video_url_four(content)
        tags = response.xpath("//a[@class='uploaderLink']/text()").extract()
        urls = response.xpath("//a[@class='thumbnailTitle']/@ href").extract()
        names = response.xpath("//a[@class='thumbnailTitle']/text()").extract()
        picture_urls = response.xpath("//img[@class='  videoThumb js-videoThumb js-videoThumbWebm']/@ src").extract()
        if len(tags) and len(urls) and len(names) and len(picture_urls):
            for i in range(len(urls)):
                self.i = self.i + 1
                yield get_video_item(id=self.i, tags=tags[-1], url=urls[i], name=names[i], pUrl=picture_urls[i], vUrl="")
        if len(video_url):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags='', url=response.url, name='', pUrl='', vUrl=video_url[0])

        url_list = get_url(content)
        for url in url_list:
            if url.startswith('/'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
            elif url.startswith('http'):
                yield scrapy.Request(url, callback=self.parse)
